{include file="../application/admin/view/header.html"}
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
	<div class="alert kesn_prompt"></div>
</div>

	<button class="btn btn-info" id="add_warehouse" data-title="新增仓库" data-id="add_warehouse">新增仓库<span class="fui-plus btn-tip"></span></button>

	 <form class="navbar-form navbar-right" action="index" role="search"  method="get">
	  <div class="form-group">
	    <div class="input-group input-group-rounded">
	      <input class="form-control" id="keyword" name="keyword" type="search" placeholder="请输入仓库名">
	      <span class="input-group-btn">
	        <button type="submit" class="btn kesn_search"><span class="fui-search"></span></button>
	      </span>
	    </div>
	  </div>
	</form>

</div><br/>
&nbsp;&nbsp;<br/>
<div></div>
<div class="row">
	<table class="table table-hover">
		<thead>
			<th>ID</th>
			<th>仓库</th>
			<th>操作</th>
		</thead>
		<tbody>
			{volist name="warehouse_list" id="warehouse"}
			<tr class="_item"> 
				<th scrope="row">{$warehouse.id}</th>
				<td class="warehouse_name">{$warehouse.warehouse}</td>
				<!-- <td><input type="text" class="form-control input-sm" placeholder="" name="warehouse_name" value="{$warehouse.warehouse}" disabled="disabled" /></td> -->
				<td class="change_button"><div>
					<a href="#" class="btn btn-info edit_warehouse btn-sm" data-status="0">编辑<span class="fui-new btn-tip"></span></a>
					<a href="#" class="btn btn-primary kesn_to btn-sm" data-id="edit_admin" data-title="{$warehouse.warehouse}用户" data-url="/index.php/admin/ouser/save_warehouse?warehouse_id={$warehouse.id}">编辑用户<span class="fui-new btn-tip"></span></a>
					<a href="#" class="btn btn-danger kesn_del btn-sm" data-_id="{$warehouse.id}" data-_name="{$warehouse.warehouse}">删除<span class="fui-cross btn-tip"></span></a>
				</div></td>
		
		</tr>
		{/volist}
	</tbody>
</div>
{$page}	
</div>

{include file="../application/admin/view/footer.html"}
<script type="text/javascript">
//删除
$('tbody').on('click','.kesn_del',function(){
	var _this = $(this);
	var _id = _this.attr('data-_id');
	var _name = _this.attr('data-_name');
	var _url = '/index.php/admin/ouser/del_warehouse';
	kesn_del(_id,_url,'您确实要删除'+_id+' : '+_name+' 吗？',_this);
});
//新增
$('#add_warehouse').click(function(){
	var _html = '<tr class="_item"> <th scrope="row"></th><td class="warehouse_name"><input type="text" class="form-control" placeholder="" name="warehouse_name" value=""/></td><td class="change_button"><div><a href="#" class="btn btn-info sure_warehouse btn-sm">确认<span class="fui-check-circle btn-tip"></span></a><a href="#" class="btn btn-default cancle_warehouse btn-sm">取消<span class="fui-trash btn-tip"></span></a></div></td></tr>';
	$('tbody').append(_html);
});
//确认
$('tbody').on('click','.sure_warehouse',function(){
	var _this = $(this);
	var _this_item = _this.parents('._item');
	var warehouse_name = _this_item.find('input[name=warehouse_name]').val();
	warehouse_name = $.trim(warehouse_name);
	if (warehouse_name == '') {
		alert('仓库名不能为空！');
		return;
	}
	  $.ajax({
	       url: "add_warehouse",    //请求的url地址
	      dataType: "json",   //返回格式为json
	      async: true, //请求是否异步，默认为异步，这也是ajax重要特性
	      data: {"warehouse_name" :warehouse_name},    //参数值
	      type: "POST",   //请求方式
	      success: function(data) {
	      	if (data.err == '0') {
	      		// _this_item.find('input[name=warehouse_name]').attr('disabled','disabled');
	      		_this_item.find('.warehouse_name').html(warehouse_name);
	      		_this_item.find('th[scrope=row]').html(data.warehouse_id);
	      		var change_button_html = '<div><a href="#" class="btn btn-info edit_warehouse btn-sm" data-status="0">编辑<span class="fui-new btn-tip"></span></a><a href="#" class="btn btn-primary kesn_to btn-sm" data-id="edit_warehouse_ouser" data-title="'+warehouse_name+'用户" data-url="/index.php/admin/ouser/save_warehouse?warehouse_id='+data.warehouse_id+'">编辑用户<span class="fui-new btn-tip"></span></a><a href="#" class="btn btn-danger kesn_del btn-sm" data-_id="'+data.warehouse_id+'" data-_name="'+warehouse_name+'">删除<span class="fui-cross btn-tip"></span></a></div>';
	        	_this_item.find('.change_button').html(change_button_html);
	        	kesn_prompt('success',data.content);
	      	}else{
	      		kesn_prompt('fail',data.content);
	      	}
	        

	      },
	  });
	
});

//取消
$('tbody').on('click','.cancle_warehouse',function(){
	var _this = $(this);
	_this.parents('._item').remove();
});

//编辑
$('tbody').on('click','.edit_warehouse',function(){
	var _this = $(this);
	var _this_item = _this.parents('._item');
	var _status = _this.attr('data-status');
	if (_status == '0') {
		// _this_item.find('input[name=warehouse_name]').removeAttr('disabled');
		var warehouse_name = $.trim(_this_item.find('.warehouse_name').html());
		_this_item.find('.warehouse_name').html('<input type="text" class="form-control input-sm" placeholder="" name="warehouse_name" value="'+warehouse_name+'"/>');
		_this.html('确认<span class="fui-check btn-tip"></span>');
		_this.attr('data-status','1');
	}else{
		var warehouse_id = _this_item.find('th[scrope=row]').html();
		var warehouse_name = _this_item.find('input[name=warehouse_name]').val();
		if ($.trim(warehouse_name) == '') {
			alert('仓库名不能为空！');
			return;
		}
		  $.ajax({
		       url: "edit_warehouse",    //请求的url地址
		      dataType: "json",   //返回格式为json
		      async: true, //请求是否异步，默认为异步，这也是ajax重要特性
		      data: {"warehouse_id":warehouse_id,"warehouse_name":warehouse_name },    //参数值
		      type: "POST",   //请求方式
		      success: function(data) {
		        if (data.err == '0') {
		        	// _this_item.find('input[name=warehouse_name]').attr('disabled','disabled');
		        	_this_item.find('.warehouse_name').html(warehouse_name);
					_this.html('编辑<span class="fui-new btn-tip"></span>');
					_this.attr('data-status','0');
					kesn_prompt('success',data.content);
		        }else{
		        	kesn_prompt('fail',data.content);
		        }
		      }, 
		});
	}
});

</script>
</footer>
</body>
</html>